package com.wz.netty.transport;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class TransportClientHandler extends ChannelInboundHandlerAdapter{

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("cli-channelInactive");
		for (int i = 0; i < 10; i++) {
			Thread.sleep(2000);
			ctx.writeAndFlush(new byte[]{1,2});
		}
		super.channelInactive(ctx);
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("cli-channelRead");
		ByteBuf m = (ByteBuf) msg;
		File f = new File("C://Users//wangzhi//Desktop//gascard-commission-backup.zip");
		//		if (!f.exists()){
		//			try {
		//				f.createNewFile();
		//			} catch (IOException e) {
		//				// TODO Auto-generated catch block
		//				e.printStackTrace();
		//			}
		//		}
		RandomAccessFile fileOutputStream = null;
		try {
			fileOutputStream = new RandomAccessFile(f,"rw");
			while(m.isReadable()){
				byte[] recevie = null;
				if (m.readableBytes()<1024){
					recevie = new byte[m.readableBytes()];
					m.readBytes(recevie, 0, m.readableBytes());
				} else {
					recevie = new byte[1024];
					m.readBytes(recevie, 0, 1024);
				}
				fileOutputStream.seek(fileOutputStream.length());
				fileOutputStream.write(recevie);
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} finally{
			System.out.println("下载文件成功");
			ReferenceCountUtil.release(m);
			fileOutputStream.close();
		}
	//			try{
	//				saveFile("C://Users//wangzhi//Desktop//pom-backup.xml", recevie);
	//			} catch(Exception e){
	//				e.printStackTrace();
	//			}
}

@Override
public void channelReadComplete(final ChannelHandlerContext ctx) throws Exception {
	System.out.println("cli-channelReadComplete");
	//		ByteBuf m = ctx.alloc().buffer(10);
	//		m.writeBytes(new String("����,�ϴ�!").getBytes());
	//		ctx.writeAndFlush(m);
	//		final ChannelFuture future = ctx.writeAndFlush(m);
	//		future.addListener(new ChannelFutureListener() {
	//			
	//			public void operationComplete(ChannelFuture future) throws Exception {
	//				ctx.close();
	//			}
	//		});
	//		ctx.close();
}

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
	System.out.println("cli-channelRegistered");
	super.channelRegistered(ctx);
}

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
	System.out.println("cli-channelUnregistered");
	super.channelUnregistered(ctx);
}

@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
	System.out.println("cli-channelWritabilityChanged");
	super.channelWritabilityChanged(ctx);
}

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
	System.out.println("cli-userEventTriggered");
	super.userEventTriggered(ctx, evt);
}

@Override  
public void channelActive(final ChannelHandlerContext ctx) throws Exception{ // (1)
	System.out.println("cli-channelActive");
	super.channelActive(ctx);
}

@Override  
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {  
	cause.printStackTrace();
	System.out.println("error");
	ctx.close();  
}
}
